package com.wooophone.webapp.mpyeah.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.wooophone.common.util.HtmlRegexpUtil;
import com.wooophone.common.web.action.BaseAction;
import com.wooophone.mpyeah.biz.BrandCommentBiz;
import com.wooophone.mpyeah.biz.BrandCommentReplyBiz;
import com.wooophone.mpyeah.entity.BrandComment;
import com.wooophone.mpyeah.entity.BrandCommentReply;

public class CommentAction extends BaseAction {

	private final static String AJAX_STATUS_REPEAT = "{'status':'repeat'}";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private BrandCommentBiz brandCommentBiz;

	@Autowired
	private BrandCommentReplyBiz brandCommentReplyBiz;

	private BrandComment comment;

	private BrandCommentReply reply;

	private List<BrandCommentReply> replys;

	public String comment() {
		try {
			regexp(comment);
			comment = brandCommentBiz.save(comment);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 去除危险标签
	 * 
	 * @param comment
	 */
	private void regexp(BrandComment comment) {
		if (comment != null) {
			if (comment.getContent() != null)
				comment.setContent(HtmlRegexpUtil.filterHtml(comment
						.getContent()));
			if (comment.getTitle() != null)
				comment.setTitle(HtmlRegexpUtil.filterHtml(comment.getTitle()));
			if (comment.getImpression() != null)
				comment.setImpression(HtmlRegexpUtil.filterHtml(comment
						.getImpression()));
		}
	}

	/**
	 * 回复
	 * 
	 * @return
	 */
	@RequiredStringValidator(fieldName = "reply.commentId", key = "id.null", message = "id is null")
	public String reply() {
		try {
			brandCommentReplyBiz.save(reply);
			if (reply != null && reply.getId() != null) {
				return ajax(Status.success);
			} else {
				return ajax(Status.error);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ajax(Status.error);
		}
	}

	@RequiredStringValidator(fieldName = "id", key = "id.null", message = "id is null")
	public String useful() {
		try {
			if (checkRepeat()) {
				if (brandCommentBiz.usefulPlusOne(id) > 0) {
					setUseSession();
					return ajax(Status.success);
				} else {
					return ajax(Status.error);
				}
			} else {
				return ajax(AJAX_STATUS_REPEAT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ajax(Status.error);
		}
	}

	@RequiredStringValidator(fieldName = "id", key = "id.null", message = "id is null")
	public String useless() {
		try {
			// 检查当前用户是否有过点击。。
			if (checkRepeat()) {
				if (brandCommentBiz.uselessPlusOne(id) > 0) {
					setUseSession();
					return ajax(Status.success);
				} else {
					return ajax(Status.error);
				}
			} else {
				return ajax(AJAX_STATUS_REPEAT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ajax(Status.error);
		}
	}

	@RequiredStringValidator(fieldName = "id", key = "id.null", message = "id is null")
	public String view() {
		comment = brandCommentBiz.get(id);
		
		if(comment==null)
			return "null";

		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("commentId", id);
		pager.setRecordCount(brandCommentReplyBiz.count(parameter));

		replys = brandCommentReplyBiz.find(parameter,
				(pager.getCurrPageNo() - 1) * pager.getPageSize(),
				pager.getPageSize());

		return SUCCESS;
	}

	private String getUseSessionKey() {
		return "use_" + id;
	}

	private void setUseSession() {
		setSession(getUseSessionKey(), 0);
	}

	/**
	 * 检查是否重复点击
	 * 
	 * @return
	 */
	public boolean checkRepeat() {
		return getSession(getUseSessionKey()) == null ? true : false;
	}

	public BrandComment getComment() {
		return comment;
	}

	public void setComment(BrandComment comment) {
		this.comment = comment;
	}

	public void setBrandCommentBiz(BrandCommentBiz brandCommentBiz) {
		this.brandCommentBiz = brandCommentBiz;
	}

	public void setBrandCommentReplyBiz(
			BrandCommentReplyBiz brandCommentReplyBiz) {
		this.brandCommentReplyBiz = brandCommentReplyBiz;
	}

	public BrandCommentReply getReply() {
		return reply;
	}

	public void setReply(BrandCommentReply reply) {
		this.reply = reply;
	}

	public List<BrandCommentReply> getReplys() {
		return replys;
	}

	public void setReplys(List<BrandCommentReply> replys) {
		this.replys = replys;
	}
}
